<template>
  <div class="login-container">
    <el-row style="height: 100%; margin-right: 2vw;">
      <el-col :lg="18" :md="12" :sm="24" :xl="18" :xs="24">
        <div style="color: transparent"></div>
      </el-col>
      <el-col :lg="6" :md="12" :sm="24" :xl="6" :xs="24">
        <userLogin v-if="activeName==='user'"></userLogin>
        <codeLogin v-else-if="activeName==='code'"></codeLogin>
      </el-col>
<!--      <el-col :lg="1" :md="1" :sm="24" :xl="1" :xs="24">-->
<!--        <div style="color: transparent"></div>-->
<!--      </el-col>-->
    </el-row>

  </div>
</template>
<script>
    import userLogin from "./userlogin.vue";
    import codeLogin from "./codelogin.vue";
    import thirdLogin from "./thirdlogin.vue";
    import {mapGetters} from "vuex";
    import {validatenull} from "@/utils/validate";
    import topLang from "@/page/index/top/top-lang.vue";
    import website from '@/config/website';
    import {getQueryString, getTopUrl} from "@/utils/util";

    export default {
        name: "login",
        components: {
            userLogin,
            codeLogin,
            thirdLogin,
            topLang
        },
        data() {
            return {
                website: website,
                time: "",
                activeName: "user",
                socialForm: {
                    tenantId: "000000",
                    source: "",
                    code: "",
                    state: "",
                },
                params: {
                    authCode: ""
                },
            };
        },
        watch: {
            $route() {
                this.handleLogin();
            }
        },
        created() {
            this.handleLogin();
            this.getTime();
        },
        mounted() {
            this.codeLogin();
        },
        computed: {
            ...mapGetters(["website", "tagWel", "userInfo"])
        },
        props: [],
        methods: {
            codeLogin() {
                // 获取URL携带的参数
                let query = window.location.search.substring(1);
                let vars = query.split("&");
                for (let i = 0; i < vars.length; i++) {
                    let pair = vars[i].split("=");
                    this.params[pair[0]] = pair[1] || "";
                }
                if (vars && vars.length > 0) {
                    window.history.replaceState("", "", window.location.href.replace(window.location.search, ""));
                }
                if (this.params.authcode) {
                    this.params.authCode = this.params.authcode;
                }
                if (this.params.clear !== undefined) {
                    this.$store.commit('SET_USER_INFO', {});
                }
                if (this.params.authCode) {
                    this.$store.commit('SET_USER_INFO', {});
                    const loading = this.$loading({
                        lock: true,
                        text: '登录中,请稍后。。。',
                        spinner: "el-icon-loading"
                    });
                    this.$store.dispatch("VerifyAuth", this.params).then(() => {
                        loading.close();
                        this.$store.commit('SET_USER_INFO', this.userInfo);
                        this.$router.push({path: this.tagWel.value});
                    }).catch(() => {
                        this.$store.commit('SET_USER_INFO', {
                            authCode: this.params.authCode
                        });
                        loading.close();
                    });
                } else {
                    // this.params.authCode = this.userInfo.authCode;
                    // if (this.params.authCode) {
                    //   this.$alert("请求未授权，请重新进入！", '提示', {
                    //     type: "warning",
                    //     center: true,
                    //     showClose: false,
                    //     showConfirmButton: false
                    //   })
                    // } else {
                    //   this.$store.commit('SET_USER_INFO', {});
                    // }
                }
            },
            getTime() {
                setInterval(() => {
                    this.time = this.$dayjs().format('YYYY-MM-DD HH:mm:ss');
                }, 1000);
            },
            handleLogin() {
                const topUrl = getTopUrl();
                const redirectUrl = '/oauth/redirect/';
                const ssoCode = '?code=';
                this.socialForm.source = getQueryString('source');
                this.socialForm.code = getQueryString('code');
                this.socialForm.state = getQueryString('state');
                if (validatenull(this.socialForm.source) && topUrl.includes(redirectUrl)) {
                    let source = topUrl.split('?')[0];
                    source = source.split(redirectUrl)[1];
                    this.socialForm.source = source;
                }
                if (
                    topUrl.includes(redirectUrl) &&
                    !validatenull(this.socialForm.source) &&
                    !validatenull(this.socialForm.code) &&
                    !validatenull(this.socialForm.state)
                ) {
                    const loading = this.$loading({
                        lock: true,
                        text: '第三方系统登录中,请稍后',
                        background: 'rgba(0, 0, 0, 0.7)',
                    });
                    this.$store
                        .dispatch('LoginBySocial', this.socialForm)
                        .then(() => {
                            window.location.href = topUrl.split(redirectUrl)[0];
                            this.$router.push(this.tagWel);
                            loading.close();
                        })
                        .catch(() => {
                            loading.close();
                        });
                } else if (
                    !topUrl.includes(redirectUrl) &&
                    !validatenull(this.socialForm.code) &&
                    !validatenull(this.socialForm.state)
                ) {
                    const loading = this.$loading({
                        lock: true,
                        text: '单点系统登录中,请稍后',
                        background: 'rgba(0, 0, 0, 0.7)',
                    });
                    this.$store
                        .dispatch('LoginBySso', this.socialForm)
                        .then(() => {
                            window.location.href = topUrl.split(ssoCode)[0];
                            this.$router.push(this.tagWel);
                            loading.close();
                        })
                        .catch(() => {
                            loading.close();
                        });
                }
            }
        }
    };
</script>

<style lang="scss" scoped>
//   @import "@/styles/login.scss";
.el-row{
    width: 100%;
}
.login-container {
  background: url(/img/bg/login_bg.jpg)center center fixed
  no-repeat;
  background-size: contain;
  display: flex;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100%;
  margin: 0 auto;
  //background-size: 100% 100%;



    :deep() {
      .vab-footer {
        position: fixed;
        bottom: 20px;
        width: 100%;
        color: #fff !important;
        text-align: center;
        background: transparent;
        border: 0;
      }
    }
  }
</style>
